<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="签订人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="applyName">
              <j-popup
                v-model="model.applyName"
                field="applyName"
                org-fields="apply_name,apply_id"
                dest-fields="applyName,applyId"
                code="apply_info"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报名编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="applyId">
              <a-input v-model="model.applyId" placeholder="请输入报名编号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractMoney">
              <a-input-number v-model="model.contractMoney" placeholder="请输入合同金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="缴费方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractPayment">
              <j-dict-select-tag type="list" v-model="model.contractPayment" dictCode="payment_method" placeholder="请选择缴费方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="签订时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signedTime">
              <j-date placeholder="请选择签订时间" v-model="model.signedTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生效时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="effectiveTime">
              <j-date placeholder="请选择生效时间" v-model="model.effectiveTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="失效时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="failureTime">
              <j-date placeholder="请选择失效时间" v-model="model.failureTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractAttachment">
              <j-upload v-model="model.contractAttachment"   ></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ApplyContractForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      let timeCheck=(rule,value,callback)=>{
        let effectiveTime = this.model.effectiveTime
        let failureTime = this.model.failureTime
        if(effectiveTime>failureTime){
          callback("合同失效时间不能小于生效时间")
        }
        callback();
      }
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           applyName: [
              { required: true, message: '请输入签订人!'},
           ],
           contractMoney: [
              { required: true, message: '请输入合同金额!'},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
           ],
           contractPayment: [
              { required: true, message: '请输入缴费方式!'},
           ],
           signedTime: [
              { required: true, message: '请输入签订时间!'},
           ],
           effectiveTime: [
              { required: true, message: '请输入生效时间!'},
           ],
           failureTime: [
              { required: true, message: '请输入失效时间!'},
             {validator:timeCheck,trigger:'change'}
           ],
        },
        url: {
          add: "/applycontract/applyContract/add",
          edit: "/applycontract/applyContract/edit",
          queryById: "/applycontract/applyContract/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>